var senderOrReceiverFlag;
var systemInterfaceParam=null;
$(document).ready(function(){
	var urlParamObj = getParamobjFromUrl();
	var fieldName = urlParamObj.fieldName? decodeURI(urlParamObj.fieldName):"";
	var systemName = urlParamObj.systemName? decodeURI(urlParamObj.systemName):"";
	var fieldId = "";

	//所属系统下拉框
	var comboxSearchSystemName = $('#comboxSearchSystemName').combobox({
	      disabled: true,
	      valueField: 'id',
	      textField: 'name',
	      editable: false,
		  data:[{id:'',name:'全部','selected':'true'}]
	  });
	//所属领域下拉框
	$('#comboxSearchfieldName').combobox({
		url: BASE_URL_FIELD_SEARCH + '/findAllOfCurrentUserCanManage' + '?v=' + Math.random(),
		valueField: 'id',
	    textField: 'name',
	    editable: false,
	    method:'get',
	    loadFilter: function(data){
	    	if(fieldName && data.content)
	    	{
	    		for(var i = 0; i < data.content.length; ++i)
	    		{
	    			if(fieldName == data.content[i].name)
	    			{
	    				fieldId = data.content[i].id;
	    				break;
	    			}
	    		}
	    	}
	    	var allCheck = {'id':'','name':'全部'};
	    	data.content.unshift(allCheck);
	    	return data.content;
	    },
	    onSelect: function(record){
	    	comboxSearchSystemName.combobox({
              disabled: false,
              method:'get',
              queryParams: {fieldId: record.id},
              url: BASE_URL_FIELD_SYSTEM_SEARCH +'/findByFieldOfCurrentUserCanManage' + '?v=' + Math.random(),
              valueField: 'id',
              textField: 'name',
			  data:'',
  		      loadFilter: function(data){
  		    	if(systemName && data.content)
  		    	{
  		    		for(var i = 0; i < data.content.length; ++i)
  		    		{
  		    			if(systemName == data.content[i].name)
  		    			{
  		    				data.content[i].selected = true;
  		    				break;
  		    			}
  		    		}
  		    	}
  		    	var allCheck = {'id':'','name':'全部'};
  		    	data.content.unshift(allCheck);
  		    	return data.content;
  		    },
  		    onLoadError: function(){
  		    	if(systemName)
  		    	{
  		    		initDatagrid();
  		    		systemName = "";
  		    	}
  				$.messager.alert("错误","<p>服务异常，查询领域系统失败，请重新登录或稍后再试！</p>","error");
  		    },
  		    onLoadSuccess:function(){
  		    	if(systemName)
  		    	{
  		    		initDatagrid();
  		    		systemName = "";
  		    	}
  		    	else
  		    	{
  		    		comboxSearchSystemName.combobox('setValue','');
  		    	}
  		    		
  		    }
          }).load();
	    },
	    onLoadError: function(){
	    	if(systemName)
	    	{
	    		initDatagrid();
	    		systemName = "";
	    	}
			$.messager.alert("错误","<p>服务异常，查询领域失败，请重新登录或稍后再试！</p>","error");
	    },
	    onLoadSuccess:function(){
	    	if(!fieldId)
	    	{
	    		$('#comboxSearchfieldName').combobox('setValue','');
	    	}
	    	else
	    	{
	    		$('#comboxSearchfieldName').combobox('select',fieldId);
	    	}
	    }
	});

	//技术标准下拉框列表
	$('#comboxSearchTechnicalStandard').combobox({
		url: BASE_URL_DATA_DICTIONARY_VALUE_SEARCH +'/findByTypeCode?typeCode='+DDTC_KETTLE_JOB_TECH_STANDARD + '&v=' + Math.random(),
		valueField: 'valueCode',
	    textField: 'valueName',
	    editable:false,
	    method:'get',
	    loadFilter: function(data){
	    	var allCheck = {'valueCode':'','valueName':'全部'};
	    	data.unshift(allCheck);
	    	return data;
	    }
	});
	
	
	//发送方数据库类型下拉框列表
	$('#comboxSearchDatabaseTypeSender').combobox({
		url: BASE_URL_DATA_DICTIONARY_VALUE_SEARCH +'/findByTypeCode?typeCode='+DDTC_DATABASE_TYPE + '&v=' + Math.random(),
		valueField: 'valueCode',
	    textField: 'valueName',
	    editable:false,
	    method:'get',
	    loadFilter: function(data){
	    	var allCheck = {'valueCode':'','valueName':'全部'};
	    	data.unshift(allCheck);
	    	return data;
	    }
	});
	//接收方数据库类型下拉框列表
	$('#comboxSearchDatabaseTypeReceiver').combobox({
		url: BASE_URL_DATA_DICTIONARY_VALUE_SEARCH +'/findByTypeCode?typeCode='+DDTC_DATABASE_TYPE + '&v=' + Math.random(),
		valueField: 'valueCode',
	    textField: 'valueName',
	    editable:false,
	    method:'get',
	    loadFilter: function(data){
	    	var allCheck = {'valueCode':'','valueName':'全部'};
	    	data.unshift(allCheck);
	    	return data;
	    }
	});
	
	//接口场景下拉框列表
    $('#comboxSearchInterfaceScan').combobox({
		url: BASE_URL_DATA_DICTIONARY_VALUE_SEARCH +'/findByTypeCode?typeCode='+DDTC_KETTLE_JOB_INTERFACE_SCENE + '&v=' + Math.random(),
		valueField: 'valueCode',
	    textField: 'valueName',
	    editable:false,
	    method:'get',
	    loadFilter: function(data){
	    	var allCheck = {'valueCode':'','valueName':'全部'};
	    	data.unshift(allCheck);
	    	return data;
	    }
	});

	if(!systemName)
	{
		initDatagrid();
	}
	
	$("#tt").tree({
		url:BASE_URL_TREE + '/getFieldSystemsForEasyUiTree' + '?v=' + Math.random(),
		lines:false,
		onLoadSuccess:function()
		{
			$("#tt > li > ul > li > .tree-node > .tree-file, #tt > li > .tree-node > .tree-file").css("background","url('../../../images/st_folder.png') no-repeat 2px 2px");
		},
		onClick:function(node){
			if(node.nodeType == 'fieldSystem'){
				if(senderOrReceiverFlag){
					$("#searchSenderSystemId").val(node.id);
					$("#txtSearchInterfaceSender").val(node.text);
					
				}else{
					$("#searchReceiverSystemId").val(node.id);
					$("#txtSearchInterfaceReceiver").val(node.text);
				}
				
				$('#interface_selection').window('close');	
			}
		}
	});
	
	//接口选择
	$('#interface_selection').window({
		width:280,
		height:350,
		modal:true,
		minimizable:false,
		maximizable:false,
		collapsible:false
	});
	$('#interface_selection').window('close');  //关闭窗口	
	$("#txtSearchInterfaceReceiver").click(function(e) {
		senderOrReceiverFlag = false;			
		$("#systemName").val('');
		$('#interface_selection').window('setTitle', '接口接收方-选择系统');
		$('#interface_selection').window('open');
		arrayLi = [];
		arrayLiInd = 0;
		$(".tree_con").scrollTop(0);
		choose_tree();
		var row = $('#tt li .tree-node');  
		row.removeClass('tree-node-selected');
		return false;
	});
	
	$("#txtSearchInterfaceSender").click(function(e) {
		senderOrReceiverFlag = true;
		
		$("#systemName").val('');
		$('#interface_selection').window('setTitle', '接口发送方-选择系统');
		$('#interface_selection').window('open');
		arrayLi = [];
		arrayLiInd = 0;
		$(".tree_con").scrollTop(0);
		choose_tree();
		var row = $('#tt li .tree-node');  
		row.removeClass('tree-node-selected');
		return false;
	});

});

//初始化表格
function initDatagrid() {
	$("#datagrid").datagrid({
		url : BASE_URL_SYSTEM_INTERFACE_SEARCH + '/findByExample' + '?v=' + Math.random(),
		method:'get',
		autoRowHeight:true,
		nowrap:false,
		singleSelect:true,
		striped:true,
		pageSize:20,
		pageList:[10,20,50,100],
		columns:[[
					{field:'fieldName',title:'领域',width:80},
					{field:'fieldSystemName',title:'系统',width:80},
					{field:'jobName',title:'接口名称',width:100,align:'left'},
					{field:'tecStandardForDisplay',title:'技术标准',width:60},
					{field:'senderFieldSystemName',title:'接口发送方',width:130},
					{field:'senderDBTypeForDisplay',title:'发送方数据库类型',width:100},
					{field:'receiverFieldSystemName',title:'接口接收方',width:130},
					{field:'receiverDBTypeForDisplay',title:'接口接收方数据库类型',width:120},
					{field:'interfaceSceneForDisplay',title:'场景',width:80}
		      ]],
		pagination:true,
		onSelect:function(rowIndex,rowData){
			$("#toolbar_edit").linkbutton('enable');
			$("#toolbar_delete").linkbutton('enable');
			$(this).datagrid('unselectRow', rowIndex);
		},
		loadFilter:function(data){
			//alert("inData:"+JSON.stringify(data));
			return {
				total: data.totalElements,
				rows: data.content
			};
		},
		onBeforeLoad:function(param){
			param.page -= 1;
			param.size = param.rows;
			delete param.rows;
			
			param.sort = 'createTime,desc';
			var fieldId = $('#comboxSearchfieldName').combobox('getValue');
			//alert("getText:"+fieldName);
			if(fieldId !=""){
				param.fieldId = fieldId;
			}
			
			
			var fieldSystemId = $('#comboxSearchSystemName').combobox('getValue');
			//alert("getText:"+fieldSystemName);
			if(fieldSystemId !=""){
				param.fieldSystemId = fieldSystemId;
			}
			
			
			
			var jobName = $('#txtSearchInterfaceName').val();
			if(jobName !=""){
				//alert(jobName);
				param.jobName = jobName;
			}
			
			
			
			var tecStandard = $('#comboxSearchTechnicalStandard').combobox('getValue');
			//alert("getText:"+fieldSystemName);
			if(tecStandard !=""){
				param.tecStandard = tecStandard;
			}
			
			var senderFieldSystem = $('#searchSenderSystemId').val();
			if(senderFieldSystem !=""){
				//alert(jobName);
				param.senderFieldSystem = senderFieldSystem;
			}
			var senderDBType = $('#comboxSearchDatabaseTypeSender').combobox('getValue');
			//alert("getText:"+fieldSystemName);
			if(senderDBType !=""){
				param.senderDBType = senderDBType;
			}
			
			
			var receiverFieldSystem = $('#searchReceiverSystemId').val();
			if(receiverFieldSystem !=""){
				//alert(jobName);
				param.receiverFieldSystem = receiverFieldSystem;
			}
			
			var receiverDBType = $('#comboxSearchDatabaseTypeReceiver').combobox('getValue');
			//alert("getText:"+fieldSystemName);
			if(receiverDBType !=""){
				param.receiverDBType = receiverDBType;
			}
			
			
			var interfaceScene = $('#comboxSearchInterfaceScan').combobox('getValue');
			//alert("getText:"+fieldSystemName);
			if(interfaceScene !=""){
				param.interfaceScene = interfaceScene;
			}
			
			systemInterfaceParam = param;
		}
	});
	
	$("#datagrid").datagrid('getPager').pagination({   
	    displayMsg:'当前显示从 [{from}] 到 [{to}] 共[{total}]条记录'
	}); 
}
	//查询
	function search(){
		$('#datagrid').datagrid({
			url:BASE_URL_SYSTEM_INTERFACE_SEARCH + '/findByExample' + '?v=' + Math.random()
		});
	}
	
	function exportData(){
		$('#datagrid').datagrid({
			url:BASE_URL_SYSTEM_INTERFACE_SEARCH + '/findByExample' + '?v=' + Math.random()
		});
		var param = JSON.stringify(systemInterfaceParam);
		location.href= BASE_URL_EXPORTDATA_FILE + '/fileInterfaceBasicInfoExport?param='+param;
	}
	
	//定位显示
	var arrayLi = [];
	var arrayLiInd = 0;
	$("#searchNode").click(function (e) { 
		if($("#systemName").val() != '')
		{
			choose_tree();
		}else
		{
			$("input").val("");
			var row = $('#tt li .tree-node');  
			row.removeClass('tree-node-selected');
			$(".tree_con").scrollTop(0);
		}
	});  
	$("#systemName").bind('input propertychange', function() {
		arrayLi = [];
		arrayLiInd = 0;
		if($("#systemName").val() != '')
		{
			choose_tree();
		}else
		{
			$("#ff input").val("");
			var row = $('#tt li .tree-node');  
			row.removeClass('tree-node-selected');
			$(".tree_con").scrollTop(0);
		}
	});
	$(document , "#systemName").keyup(function(event){
	  if(event.keyCode ==13){
	    $("#searchNode").trigger("click");
	  }
	});
	
	$("#emptyNode").click(function (e){
		if(senderOrReceiverFlag)
		{
			$("#txtSearchInterfaceSender, #searchSenderSystemId").val("");
			
		}else
		{
			$("#txtSearchInterfaceReceiver, #searchReceiverSystemId").val("");
		}
		$('#interface_selection').window('close');
	});
	
	function getSelected()
	{
		var node = $('#tt').tree('getSelected');
		if(node.nodeType == 'systemInterface'){
			if(senderOrReceiverFlag){
				$("#searchSenderSystemId").val(node.id);
				$("#txtSearchInterfaceSender").val(node.text);
				
			}else{
				$("#searchReceiverSystemId").val(node.id);
				$("#txtSearchInterfaceReceiver").val(node.text);
			}
			
			/* $('#interface_selection').window('close'); */
		}
	}
	//数据检索区域重置方法
	function onResetOwn() {
		$(".sel_con input").not("input[readonly], .combobox-f").each(function(){
			$(this).val("");
		});
		$("#searchSenderSystemId,#txtSearchInterfaceSender,#searchReceiverSystemId,#txtSearchInterfaceReceiver").val("");
		$(".combobox-f").combobox('setValue','');
		$(".easyui-datetimebox").combo('setValue', '').combo('setText', '');
	}
	
	function choose_tree()
	{
		$('#tt').tree('expandAll');
		if(arrayLi.length == 0 && arrayLiInd == 0)
		{
			var searchVal = $("#systemName").val().toUpperCase(); 
			var row = $('#tt li .tree-node');  
			var rowcolumn = $('#tt li .tree-node > .tree-title');  
			var i = 0, j = 0;
			var offsetC = 0
			row.removeClass('tree-node-selected');  
			rowcolumn.each(function (column) {  
				var matchVal = $(this).text(); 
				if(matchVal.indexOf(searchVal) != -1)
				{
					i++;
					var panel_top = parseInt($(this).parents(".panel").css("top"));
					
					if(i <= 1)
					{
						$(this).parent().addClass('tree-node-selected'); 
						var top = $(".tree_con").scrollTop() + $(this).offset().top - 74 - panel_top;
						$(".tree_con").scrollTop(top);
						getSelected();
					}else
					{				
						arrayLi.push([matchVal,$(".tree_con").scrollTop() + $(this).offset().top - 74 - panel_top]);
					}
					
				}
				}); 
			}else
			{
				var rowcolumn = $('#tt li .tree-node > .tree-title'); 
				var row = $('#tt li .tree-node');  
				row.removeClass('tree-node-selected');
				
				if(arrayLiInd <  arrayLi.length)
				{
					rowcolumn.each(function (column) {  
						var matchVal = $(this).text(); 
						if(matchVal == arrayLi[arrayLiInd][0])
						{
							$(this).parent().addClass('tree-node-selected'); 
							$(".tree_con").scrollTop(arrayLi[arrayLiInd][1]);
							arrayLiInd++;
							getSelected();
							return false;
						}
					});
				}else
				{
					arrayLi = [];
					arrayLiInd = 0;
					$(".tree_con").scrollTop(0);
					choose_tree();
				}
			}
	}